package com.msh.automatic.android.control.script.task

import android.accessibilityservice.AccessibilityService
import android.content.ActivityNotFoundException
import android.content.Intent
import android.provider.Settings
import cn.hutool.core.util.StrUtil
import com.alibaba.fastjson2.JSONObject
import com.msh.automatic.android.control.script.service.ScriptAccessibilityService
import com.msh.automatic.android.control.script.util.AccessibilityServiceUtil
import com.msh.automatic.android.control.script.util.TaskApi
import com.msh.automatic.android.control.script.util.TaskLogStatus
import com.msh.automatic.android.control.script.util.TaskUtil
import kotlinx.coroutines.delay
import kotlin.system.exitProcess

class check_vpn_status
    (task: JSONObject, taskLog: JSONObject, token: String) :
    Task(task, taskLog, token)
{
    override suspend fun run(service: ScriptAccessibilityService): Boolean {
        AccessibilityServiceUtil.launchApp(service,"net.kaaass.zerotierfix")
        if (!AccessibilityServiceUtil.waitActivity(service,"net.kaaass.zerotierfix","net.kaaass.zerotierfix.ui.NetworkListActivity",3)){
            TaskApi.log(service,this.getTaskLogId(), TaskLogStatus.over,"无法打开vpn 界面, ${service.currentPackage}   ${service.currentActivity}");
            return false
        }
        val node = AccessibilityServiceUtil.findAccessibilityNodeInfosByText(service,"网络名").get(0).parent.getChild(4)
        if(node.stateDescription != "开启"){
            AccessibilityServiceUtil.clickByNode(service,node)
        }
        TaskApi.log(service,this.getTaskLogId(),TaskLogStatus.over,"")
        return true
    }
}